//---------------------------------------------------------------------
// Authors: jachymko
//
// Description: Class implementing the New-SymLink command.
//
// Creation Date: Dec 13, 2006
//
//---------------------------------------------------------------------

using System;
using System.ComponentModel;
using System.IO;
using System.Management.Automation;

namespace Pscx.Commands.FileSystem.Ntfs
{
    using Interop;

    [Cmdlet(VerbsCommon.New, PscxNouns.Symlink)]
    [Description("Creates filesystem symbolic links. Requires Microsoft Windows Vista or later.")]
    public partial class NewSymLinkCommand : NewLinkCommandBase
    {
        protected override void BeginProcessing()
        {
            Utils.EnsureWindowsVista(this);
        }
        
        protected override void ProcessRecord()
        {
            FileSystemInfo target = FindFileOrDirectory(FullTarget);
            if(target == null)
            {
                WriteError(PscxErrorRecord.FileNotFoundError(Target));
                return;
            }

            int flags = 0;

            if(target is DirectoryInfo)
            {
                flags = NativeMethods.SYMLINK_FLAG_DIRECTORY;
            }

            if(!NativeMethods.CreateSymbolicLink(FullPath, FullTarget, flags))
            {
                WriteError(PscxErrorRecord.LastWin32Error("CreateSymbolicLinkError", ErrorCategory.NotSpecified, Path   ));
                return;
            }

            WriteObject(FindFileOrDirectory(FullPath));           
        }
    }
}
